<?php

namespace backend\models\course;

use Yii;
use backend\models\course\Course;
use backend\models\course\CourseOrderInfo;
/**
 * This is the model class for table "{{%edu_course_order}}".
 *
 * @property integer $order_id
 * @property integer $order_sn
 * @property integer $user_id
 * @property string $user_name
 * @property integer $user_phone
 * @property string $user_email
 * @property string $price_amount
 * @property integer $pay_type
 * @property integer $pay_time
 * @property integer $order_addtime
 * @property integer $order_status
 */
class CourseOrder extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%course_order}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['user_id', 'user_phone', 'pay_type', 'pay_time', 'order_addtime', 'order_status'], 'integer'],
            [['price_amount'], 'number'],
            [['user_name', 'user_email'], 'string', 'max' => 30],
            [['order_sn'],'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'order_id' => 'Order ID',
            'order_sn' => 'Order Sn',
            'user_id' => 'User ID',
            'user_name' => 'User Name',
            'user_phone' => 'User Phone',
            'user_email' => 'User Email',
            'price_amount' => 'Price Amount',
            'pay_type' => 'Pay Type',
            'pay_time' => 'Pay Time',
            'order_addtime' => 'Order Addtime',
            'order_status' => 'Order Status',
            'amount' =>'Amount',
            'coinAmount' => 'coinAmount',
            'coinRate' => 'coinRate',
            'type' =>'Type',

        ];
    }
	/**
	* 两表联查
	*/
	public function getCourseorderinfo()
    {
		return $this->hasMany(CourseOrderInfo::className(), ['order_id'=>'order_id']);
	}
	/**
	* 查询
	*/
	public function Order($order_id)
    {
		return $this->find()
				->joinwith('courseorderinfo')
				->where("edu_course_order.order_id = $order_id")
				->asArray()
				->one();
	}
	/**
	* 查询
	*/
	public function OrderCount($code)
    {
		return $this->find()
					->where($code)
					->count();
	}
	/**
	* 搜索查询
	*/
	public function OrderSearch($code,$pagination)
    {
		return $this->find()
				->where($code)
				->orderBy(['order_addtime'=>SORT_DESC])
				->offset($pagination->offset)
				->limit($pagination->limit)
				->asArray()
				->all();
	}

	static public function TodayRevenue($today_start,$today_end)
	{
		return CourseOrder::find()
				->sum("price_amount");
	}

	/* 营收额  昨日 */
	static public function YesterdayRevenue($yesterday_start,$yesterday_end)
	{
		return CourseOrder::find()
				->where("pay_time >= :yesterday_start and pay_time <= :yesterday_end and order_status = :order_status",
				[':yesterday_start'=>$yesterday_start,':yesterday_end'=>$yesterday_end,'order_status'=>1])
				->sum("price_amount");
	}
}
